package com.wms.presenters;

import java.sql.SQLException;
import com.gatech.wms.R;
import com.wms.helper.DialogFactory;
import com.wms.helper.UserException;
import com.wms.model.LoginModel;
import com.wms.assets.NewUser;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.AlertDialog;

/**
 * RegisterActivity is the loose presenter for the registration screen/view in the application.
 * @author Tasmia
 */
public class RegisterActivity extends WMSActivity {
	
	private LoginModel model;
	private DialogFactory dFactory;
	/**
	 * This method initializes the screen and instantiates all necessary helper classes
	 *
	 * @param savedInstanceState
	 */
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.registration_screen);
		dFactory = new DialogFactory(this);
		model = new LoginModel();
		setupButton();
	}

	/**
	 *	This method gets a reference to the buttons and sets click listeners for every button
	 */
	private void setupButton() {
		// Get a reference to the buttons
		Button submit = (Button)findViewById(R.id.button1);  
		Button cancel = (Button)findViewById(R.id.button2);   
		//Set the onClickListener to run the code
		submit.setOnClickListener(new View.OnClickListener(){
			
			@Override
			public void onClick(View v) {
				final EditText usertext = (EditText)findViewById(R.id.editText2);
				EditText pwtext = (EditText)findViewById(R.id.editText1);
				EditText pwvtext = (EditText)findViewById(R.id.editText3);
				final String username = usertext.getText().toString();
				final String password = pwtext.getText().toString();
				final String password2 = pwvtext.getText().toString();
				try {
					if (!password.equals(password2)) {
						final AlertDialog dlog = dFactory.alertOK("Passwords do not match!", "Error!");
						dlog.show();
					}
					else {
						model.createUser(new NewUser(username, password));
						Toast.makeText(getApplicationContext(), "User added successfully!", Toast.LENGTH_LONG).show();
					}					
				} catch (SQLException e) {
					final AlertDialog dlog = dFactory.alertOK("Error registering user!", "Error");
					dlog.show();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					final AlertDialog dlog = dFactory.alertOK("Error registering user!", "Error");
					dlog.show();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					final AlertDialog dlog = dFactory.alertOK("Error registering user!", "Error");
					dlog.show();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					final AlertDialog dlog = dFactory.alertOK("Error registering user!", "Error");
					dlog.show();
				} catch (UserException e) {
					final AlertDialog dlog = dFactory.alertOK("Error registering user!", "Error");
					dlog.show();
				}
			}
			
		});
		
		cancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				RegisterActivity.this.finish();
				
			}
		});
		
	}

	
	

}
